package com.wtwd.device.util;

import com.alibaba.fastjson.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zdl
 * @Description
 * @create 2021-08-04 19:49
 */
public class StringUtil {
    /**
     * 正则表达式：验证中国手机号
     */
    public static final String REGEX_MOBILE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
    public static final String REGEX_CHINA_PHONE = "^((\\+86)|(86))?[1][3456789][0-9]{9}$";

    public static final String REGEX_NUMBER = "^\\d{3,15}$";
    //public static final String REGEX_EMAIL = "^(\\w+([-.][A-Za-z0-9]+)*){3,18}@\\w+([-.][A-Za-z0-9]+)*\\.\\w+([-.][A-Za-z0-9]+)*$";
    public static final String REGEX_EMAIL = "^([A-Za-z0-9_\\-\\.])+\\@([A-Za-z0-9_\\-\\.])+\\.([A-Za-z]{2,4})$";
    private static String REGEX_TEMPLATE_PARAM = "\\$\\{(.+?)\\}";//定义正则表达式

    private static Pattern patten = Pattern.compile(REGEX_TEMPLATE_PARAM);

    /**
     * 字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean isEmpty(String str) {
        return str == null || "".equals(str);
    }

    public static boolean isEmpty(String... strs) {
        for (String s : strs) {
            if (isEmpty(s))
                return true;
        }
        return false;
    }

    /**
     * 字符串是否不为空
     *
     * @param str
     * @return
     */
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    /**
     * 去掉前后空格后字符串是否为空
     *
     * @param str
     * @return
     */
    public static boolean trimAndIsEmpty(String str) {
        return str == null || "".equals(str.trim());
    }


    // 判断一个字符是否是中文
    public static boolean isChinese(char c) {
        return c >= 0x4E00 && c <= 0x9FA5;// 根据字节码判断
    }

    // 判断一个字符串是否含有中文
    public static boolean isChinese(String str) {
        if (str == null) return false;
        for (char c : str.toCharArray()) {
            if (isChinese(c)) return true;// 有一个中文字符就返回
        }
        return false;
    }

    /**
     * 数组转字符串
     *
     * @param var0
     * @param var1
     * @return
     */
    public static String join(String[] var0, String var1) {
        StringBuffer var2 = new StringBuffer();
        for (String s : var0) {
            if (StringUtil.isNotEmpty(s)) {
                var2 = var2.length() == 0 ? var2.append(s) : var2.append(var1 + s);
            }
        }
        return var2.toString();
    }

    public static boolean isChinaPhone(String phone) {
        if (isEmpty(phone)) return false;
        return Pattern.matches(REGEX_CHINA_PHONE, phone);
    }

    public static boolean isNotNumber(String s) {
        if (isEmpty(s)) return true;
        return !Pattern.matches(REGEX_NUMBER, s);
    }

    public static boolean isNotEmail(String email) {
        return isEmpty(email) ? true : !Pattern.matches(REGEX_EMAIL, email);
    }

    public static String replaceTemplateParams(String content, String paramsJson) {
        JSONObject params = JSONObject.parseObject(paramsJson);
        Matcher matcher = patten.matcher(content);
        StringBuffer s = new StringBuffer();
        while (matcher.find()) {
             matcher.appendReplacement(s,params.getString(matcher.group(1)));
        }
        return  matcher.appendTail(s).toString();
    }

}
